<template>
  <div>
    <div class="commentItem">
      <div class="commentItem-avatar">
        <img
          v-if="commentData.weixin_headimgurl != null"
          :class="type"
          :src="commentData.weixin_headimgurl"
        />
        <rt-avatar
          v-else
          :size="type == 'first' ? 36 : 20"
          background-color="rgba(180,189,207,1)"
          >{{ commentData.comment_by_name | fistChar }}</rt-avatar
        >
      </div>
      <div class="commentItem-info">
        <div class="infoTitle">
          <span class="name">{{ commentData.comment_by_name }}</span>
          <span class="time">{{
            commentData.CreatedOn | moment('YYYY-MM-DD HH:mm')
          }}</span>
          <rt-icon
            class="icon"
            name="rt-xrm-moremenu"
            @click.native="onShow = !onShow"
          ></rt-icon>
        </div>
        <rt-actionsheet
          v-model="onShow"
          :menus="menus"
          :show-cancel="true"
          class="actionsheet"
          @on-click-menu="handleActionsheetClick"
        />
        <div class="comment">{{ commentData.text }}</div>
      </div>
    </div>
    <slot />
  </div>
</template>

<script>
export default {
  name: 'CommentItem',
  filters: {
    fistChar: function(value) {
      if (!rt.isNullOrWhiteSpace(value)) {
        return value[0];
      } else {
        return value;
      }
    }
  },
  props: {
    commentData: {
      type: Object,
      required: true
    },
    type: {
      type: String,
      default: 'first'
    },

    tenantCode: {
      type: String,
      required: true
    },
    object_id: {
      type: String,
      required: true
    },
    object_name: {
      type: String,
      required: true
    },
    object_type_name: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      onShow: false,
      itemId: rt.newGuid(),
      menus: [
        {
          label: '回复',
          type: 'replay'
        },
        {
          label: '删除',
          type: 'del'
        }
      ]
    };
  },
  watch: {
    onShow: function(newValue) {
      this.$emit('on-action-changed', newValue);
    }
  },
  methods: {
    handleActionsheetClick(key, menu) {
      if (menu.type === 'del') {
        this.deleteComment(this.commentData.Id);
      } else if (menu.type === 'replay') {
        this.goComment(
          this.commentData.parent_id
            ? this.commentData.parent_id
            : this.commentData.Id,
          this.commentData.comment_by_id,
          this.commentData.comment_by_name
        );
      }
    },
    async goComment(
      parentDmEdaCommentId = '',
      replyContactId = '',
      replyContactIdName = ''
    ) {
      if (this.onShow) {
        this.onShow = false;
      }
      const userInfo = await rt.get('api/UserInfo/GetCurrentUserInfo');
      // 已注册
      this.$router.push({
        name: 'comment',
        params: {
          tenantCode: this.tenantCode,
          object_id: this.object_id,
          object_name: this.object_name,
          object_type_name: this.object_type_name,
          comment_by_Id: userInfo.Id,
          comment_by_name: userInfo.name,
          parent_Id: parentDmEdaCommentId,
          reply_comment_by_Id: replyContactId,
          reply_comment_by_name: replyContactIdName
        }
      });
    },
    deleteComment(Id) {
      rt.dmGet(
        `api/DmPostComment/delCommentForJcy?postId=${Id}&tenantCode=${this.tenantCode}`
      )
        .then(resp => {
          if (resp) {
            if (this.onShow) {
              this.onShow = false;
            }
            rt.showSuccessToast('评论已删除');
            this.$bus.$emit('reload-commentDatas');
          } else {
            rt.showSuccessToast('删除失败');
          }
        })
        .catch(error => {
          rt.showErrorToast(error);
        });
    }
  }
};
</script>
<style lang="less" scoped>
.commentItem {
  display: flex;
  padding-top: 15px;
  .commentItem-avatar {
    width: 36px;
    height: 36px;
    text-align: right;
    .first {
      width: 36px;
      height: 36px;
      border-radius: 18px;
    }
    .second {
      width: 20px;
      height: 20px;
      border-radius: 10px;
    }
  }
  .commentItem-info {
    width: 87%;
    border-bottom: 1px solid #f0f0f0;
    padding-bottom: 20px;
    margin-left: 8px;
    .comment {
      font-size: 12px;
      word-wrap: break-word;
    }
    .infoTitle {
      .name {
        color: #555555;
        font-size: 14px;
      }
      .time {
        font-size: 12px;
        color: #cccccc;
        margin-left: 10px;
      }
      .icon {
        float: right;
        padding-right: 15px;
      }
    }
  }
  .comment-fab {
    position: absolute;
    right: 5px;
    background-color: #ffffff;
    margin: 0px 10px;
    border-radius: 5px;
    padding: 0 10px 0 10px;
    z-index: 10;

    .comment-fab-item {
      display: flex;
      align-items: center;
      line-height: 1;
      padding: 10px 15px 10px 10px;

      p {
        font-size: 15px;
        color: #555;
        margin-left: 10px;
      }
    }

    &::before {
      content: '';
      width: 0px;
      height: 0px;
      border-left: 6px solid transparent;
      border-right: 6px solid transparent;
      border-bottom: 6px solid #ffffff;
      position: absolute;
      top: -6px;
      right: 10px;
    }
  }
}
.actionsheet {
  /deep/ .rt-actionsheet-cell-del {
    color: #ff440e;
  }
  /deep/ .rt-actionsheet-cell-replay {
    color: #222;
  }
  /deep/ .rt-actionsheet-cell {
    font-size: 15px;
  }
  /deep/ .rt-popup-mask {
    top: unset;
    z-index: unset;
  }
  /deep/ .rt-popup-content {
    position: unset;
    line-height: unset;
  }
}
</style>
